﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using InseSoft.Infraestructure;
using InseSoft.Infraestructure.Aspects;
using InseSoft.Models;
using InseSoft.Models.Entities;
using NLog;

namespace InseSoft.ViewModels
{
    public class CotizacionViewModel : EntityViewModel<Cotizacion>
    {
        #region Declarations

        private IEnumerable<CatalogEntity> clientes;
        private IEnumerable<CatalogEntity> vendedores;
        private IEnumerable<CatalogEntity> lineas;
        private IEnumerable<CatalogEntity> materiales;

        #endregion

        #region Properties

        public new Cotizacion CurrentEntity { get; set; }
        public IEnumerable<CatalogEntity> Clientes
        {
            get { return clientes; }
            set
            {
                clientes = value;
                OnPropertyChanged(() => Clientes);
            }
        }
        public IEnumerable<CatalogEntity> Vendedores
        {
            get { return vendedores; }
            set
            {
                vendedores = value;
                OnPropertyChanged(() => Vendedores);
            }
        }
        public IEnumerable<CatalogEntity> Lineas
        {
            get { return lineas; }
            set
            {
                lineas = value;
                OnPropertyChanged(() => Lineas);
            }
        }
        public IEnumerable<CatalogEntity> Materiales
        {
            get { return materiales; }
            set
            {
                materiales = value;
                OnPropertyChanged(() => Materiales);
            }
        }
        public ICommand SearchCommand
        {
            get { return new RelayCommand(SearchExecute); }
        }

        private void SearchExecute()
        {
            throw new NotImplementedException();
        }

        #endregion
        
        #region Methods

        public override void Initialize()
        {
            PopulateDropdowns();
            base.Initialize();
        }

        private async void PopulateDropdowns()
        {
            await PopulateList(() => Clientes = DbContext.Clientes.DropdownList().ToList());
            await PopulateList(() => Vendedores = DbContext.Vendedores.DropdownList().ToList());
            await PopulateList(() => Lineas = DbContext.Lineas.DropdownList().ToList());
            await PopulateList(() => Materiales = DbContext.Materiales.DropdownList().ToList());
        }

        private static Task PopulateList(Action action)
        {
            return Task.Run(action);
        }

        #endregion
    }
}
